package zj.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zj.com.model.Project;
import zj.com.model.User;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class JcrRightAdapter extends BaseAdapter{

    public List<User> list;
    public Context context;
    public LayoutInflater inflater;

    public boolean isEdit = false;
    public Map<String,User> selectMap = new HashMap<String,User>();

    public JcrRightAdapter(List<User> list, Context context){

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_select_item, null);
            viewHolder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
            viewHolder.view = convertView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        User user = list.get(position);

        if(isEdit){
            if(selectMap.get(user.getUserID()) != null){
                viewHolder.view.setBackgroundColor(Color.parseColor("#e8e8e8"));

            }else{
                viewHolder.view.setBackgroundColor(Color.WHITE);

            }
            viewHolder.name_tv.setText(user.getUserName());
        }else{
            viewHolder.name_tv.setText(user.getUserName());
        }


        return convertView;
    }
    private static class ViewHolder {
        TextView name_tv;
        View view;

    }

    public void setIsEdit(boolean isEdit){

        this.isEdit = isEdit;
    }

    public void setSelect(int position){



        User user = list.get(position);
        if(selectMap.get(user.getUserID()) == null){
            selectMap.put(user.getUserID(),user);
        }else{
            selectMap.remove(user.getUserID());
        }

        notifyDataSetChanged();

    }

    public Map<String,User> getSelectMap(){

        return selectMap;
    }

    public void putSelectMap(User user){

        selectMap.put(user.getUserID(),user);
    }

    public void setDataChange(List<User> list){


        this.list = list;
        notifyDataSetChanged();
    }

}
